package cn.bellychang.leetcode.question239;

/**
 * @author ChangLiang
 * @date 2021/3/17
 */
public class Solution {

    public int[] maxSlidingWindow(int[] nums, int k) {

        int[] result = new int[nums.length - k + 1];

        int beginIndex;
        int endIndex;

        // time complexion: O(N * K)
        for (int i = 0; i < (nums.length - k + 1); i++) {
            beginIndex = i;
            endIndex = i + k - 1;
            result[i] = getMax(nums, beginIndex, endIndex);
        }

        return result;
    }


    private int getMax(int[] nums, int beginIndex, int endIndex) {
        int max = nums[beginIndex];
        for (int i = beginIndex; i <= endIndex; i++) {
            if (max < nums[i]) {
                max = nums[i];
            }
        }
        return max;
    }
}
